<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="资源集合" prop="resourceIds">
      <el-input v-model="dataForm.resourceIds" placeholder="资源集合"></el-input>
    </el-form-item>
    <el-form-item label="客户端密钥" prop="clientSecret">
      <el-input v-model="dataForm.clientSecret" placeholder="客户端密钥"></el-input>
    </el-form-item>
    <el-form-item label="授权域" prop="scope">
      <el-input v-model="dataForm.scope" placeholder="授权域"></el-input>
    </el-form-item>
    <el-form-item label="授权方式" prop="authorizedGrantTypes">
      <el-input v-model="dataForm.authorizedGrantTypes" placeholder="授权方式"></el-input>
    </el-form-item>
    <el-form-item label="跳转地址" prop="webServerRedirectUri">
      <el-input v-model="dataForm.webServerRedirectUri" placeholder="跳转地址"></el-input>
    </el-form-item>
    <el-form-item label="权限集合" prop="authorities">
      <el-input v-model="dataForm.authorities" placeholder="权限集合"></el-input>
    </el-form-item>
    <el-form-item label="token有效期" prop="accessTokenValidity">
      <el-input v-model="dataForm.accessTokenValidity" placeholder="token有效期"></el-input>
    </el-form-item>
    <el-form-item label="refresh有效期" prop="refreshTokenValidity">
      <el-input v-model="dataForm.refreshTokenValidity" placeholder="refresh有效期"></el-input>
    </el-form-item>
    <el-form-item label="授权信息" prop="additionalInformation">
      <el-input v-model="dataForm.additionalInformation" placeholder="授权信息"></el-input>
    </el-form-item>
    <el-form-item label="其他" prop="autoapprove">
      <el-input v-model="dataForm.autoapprove" placeholder="其他"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          clientId: 0,
          resourceIds: '',
          clientSecret: '',
          scope: '',
          authorizedGrantTypes: '',
          webServerRedirectUri: '',
          authorities: '',
          accessTokenValidity: '',
          refreshTokenValidity: '',
          additionalInformation: '',
          autoapprove: ''
        },
        dataRule: {
          resourceIds: [
            { required: true, message: '资源集合不能为空', trigger: 'blur' }
          ],
          clientSecret: [
            { required: true, message: '客户端密钥不能为空', trigger: 'blur' }
          ],
          scope: [
            { required: true, message: '授权域不能为空', trigger: 'blur' }
          ],
          authorizedGrantTypes: [
            { required: true, message: '授权方式不能为空', trigger: 'blur' }
          ],
          webServerRedirectUri: [
            { required: true, message: '跳转地址不能为空', trigger: 'blur' }
          ],
          authorities: [
            { required: true, message: '权限集合不能为空', trigger: 'blur' }
          ],
          accessTokenValidity: [
            { required: true, message: 'token有效期不能为空', trigger: 'blur' }
          ],
          refreshTokenValidity: [
            { required: true, message: 'refresh有效期不能为空', trigger: 'blur' }
          ],
          additionalInformation: [
            { required: true, message: '授权信息不能为空', trigger: 'blur' }
          ],
          autoapprove: [
            { required: true, message: '其他不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.clientId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.clientId) {
            this.$http({
              url: this.$http.adornUrl(`/yunxi/oauthclientdetails/info/${this.dataForm.clientId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.resourceIds = data.oauthClientDetails.resourceIds
                this.dataForm.clientSecret = data.oauthClientDetails.clientSecret
                this.dataForm.scope = data.oauthClientDetails.scope
                this.dataForm.authorizedGrantTypes = data.oauthClientDetails.authorizedGrantTypes
                this.dataForm.webServerRedirectUri = data.oauthClientDetails.webServerRedirectUri
                this.dataForm.authorities = data.oauthClientDetails.authorities
                this.dataForm.accessTokenValidity = data.oauthClientDetails.accessTokenValidity
                this.dataForm.refreshTokenValidity = data.oauthClientDetails.refreshTokenValidity
                this.dataForm.additionalInformation = data.oauthClientDetails.additionalInformation
                this.dataForm.autoapprove = data.oauthClientDetails.autoapprove
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/yunxi/oauthclientdetails/${!this.dataForm.clientId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'clientId': this.dataForm.clientId || undefined,
                'resourceIds': this.dataForm.resourceIds,
                'clientSecret': this.dataForm.clientSecret,
                'scope': this.dataForm.scope,
                'authorizedGrantTypes': this.dataForm.authorizedGrantTypes,
                'webServerRedirectUri': this.dataForm.webServerRedirectUri,
                'authorities': this.dataForm.authorities,
                'accessTokenValidity': this.dataForm.accessTokenValidity,
                'refreshTokenValidity': this.dataForm.refreshTokenValidity,
                'additionalInformation': this.dataForm.additionalInformation,
                'autoapprove': this.dataForm.autoapprove
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
